package com.sun.controller;

import com.alibaba.fastjson.JSON;
import com.sun.entity.FineUtil;
import com.sun.entity.Reader;
import com.sun.service.FineService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author SunRui
 * @description TODO
 * @date 2024/8/10 22:46
 */
@Controller
@RequestMapping("/FineController")
public class FineController {
    @Autowired
    private FineService fineService;

    private static Logger logger = Logger.getLogger(BookController.class);
    private String jsonString;
    private String str;

    @RequestMapping("/fineTabShow")
    public void fineTabShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询罚款缴纳记录
        HttpSession session = req.getSession();
        int page = Integer.parseInt(req.getParameter("page"));
        logger.debug("查询罚款起始页数:" + page);
        Reader reader = (Reader) session.getAttribute("reader");
        int readerId = reader.getId();
        List<FineUtil> fineUtil = fineService.getFineList(((page - 1) * 5), readerId);
        jsonString = JSON.toJSONString(fineUtil);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/finePageShow")
    public void finePageShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取总页数
        HttpSession session = req.getSession();
        Reader reader = (Reader) session.getAttribute("reader");
        int readerId = reader.getId();
        int allPage = fineService.getAllPage(readerId);
        int pages;
        if (allPage%5>0){
            pages = (allPage/5)+1;
        }else {
            pages = allPage/5;
        }
        jsonString = JSON.toJSONString(pages);
        resp.getWriter().write(jsonString);
    }
}